/*
 * Prngd_Server.h
 *
 *  Last edited: 21.08.2014
 *  Author: David Noack
 */

#ifndef PRNGD_SERVER_H_
#define PRNGD_SERVER_H_


#include "Tcp_Server.h"
#include "Udp_Server.h"
#include "Uds_Server.h"
#include "MQ_Server.h"


/**
\class Prngd_Server
\brief Main server programm to run prngd as tcp, udp, uds or mq server (or all togehter).

Prngd_Server can start up to five Instances of Prngd while every instance got another communication interface (including a local one).

Example: ./prngd_server -i tcp -imq -i udp start the prngd_server with three instances of prngd. One with tcp, one with udp and one with udp.

Example: ./prngd_server -o prngd_out -bbs 1024 -s 123456 -i local  starts the server just like a local prngd and writes the result to prngd_out.
*/
class Prngd_Server{

	public:

		/**Constructor*/
		Prngd_Server()
		{
			localserver = NULL;
			tcpserver = NULL;
			udpserver = NULL;
			udsserver = NULL;
			mqserver = NULL;

			tcp_up = false;
			udp_up = false;
			uds_up = false;
			mq_up = false;
		};

		/**Destructor*/
		~Prngd_Server()
		{
			if(localserver != NULL)
				delete localserver;
			if(tcpserver != NULL)
				delete tcpserver;
			if(udpserver != NULL)
				delete udpserver;
			if(udsserver != NULL)
				delete udsserver;
			if(mqserver != NULL)
				delete mqserver;
		};

		

		/** Starts all the selected server instances of prngd
		\param argc Number of Parameters.
		\param argv Two Dim array with all the parameters.
		*/
		void start(int argc, char** argv);


	private:

		/** local prngd server*/
		Prngd* localserver;
		/** prngd server for tcp communication*/
		Tcp_Server* tcpserver;
		/** prngd server for udp communication*/
		Udp_Server* udpserver;
		/** prngd server for unix domain socket communication*/
		Uds_Server* udsserver;
		/** prngd server for communication over message queues*/
		MQ_Server* mqserver;

		/** flag for signaling prngd tcp server is up or not*/
		bool tcp_up;
		/** flag for signaling prngd udp server is up or not*/
		bool udp_up;
		/** flag for signaling prngd uds server is up or not*/
		bool uds_up;
		/** flag for signaling prngd mq server is up or not*/
		bool mq_up;

		/** Read the command line parameters and set a return code to signal what server should be started.
		\param argc Number of Parameters.
		\param argv Two Dim array with all the parameters.
		\return code which signals what server instances have to be started.*/
		int readParameters(int argc, char** argv);

		/** Method for polling. Checks if any interfaces (a server instance) up.
		\return True if at least one interface is up, false if no interface is up.*/
		bool isAInterfaceUp()
		{
			if(tcp_up || udp_up || uds_up || mq_up)
				return true;
			else
				return false;
		}

		/** Method for polling and checking all active server instances*/
		void checkAllInterfaces()
		{
			if(tcpserver != NULL)
				tcp_up = tcpserver->isActive();
			else
				tcp_up = false;

			if(udpserver != NULL)
				udp_up = udpserver->isActive();
			else
				udp_up = false;

			if(udsserver != NULL)
				uds_up = udsserver->isActive();
			else
				uds_up = false;

			if(mqserver != NULL)
				mq_up = mqserver->isActive();
			else
				mq_up = false;
		}


};


#endif /* PRNGD_SERVER_H_ */
